package exam.test12;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2020-11-11
 * Time: 3:23
 */
public class External {

    public int externalLength(BinaryNode r) {
        return externalLength(r, 0);
    }

    private int externalLength(BinaryNode r, int d) {
        if (r == null) {
            return 0;
        }

        if (r.isLeaf()) {
            return d;
        }

        return externalLength(r.getLeftChild(), d + 1) + externalLength(r.getRightChild(), d + 1);
    }

    public static void main(String[] args) {
        External program =  new External();

        BinaryNode r = new BinaryNode(0, new BinaryNode(0), new BinaryNode(0));
        BinaryNode p = r.getRightChild();
        p.setRightChild(new BinaryNode(0));
        p.setLeftChild(new BinaryNode(0, new BinaryNode(0), new BinaryNode(0)));

        int result = program.externalLength(r, 0);
        System.out.println(result);

    }
}
